package com.wisdudu.blank.ui.common;

import android.content.Intent;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.databinding.ObservableList;

import com.kelin.mvvmlight.base.ViewModel;
import com.kelin.mvvmlight.collectionadapter.ItemView;
import com.kelin.mvvmlight.command.ReplyCommand;
import com.wisdudu.blank.R;
import com.wisdudu.blank.data.Injection;
import com.wisdudu.blank.data.bean.PhotoEntity;
import com.wisdudu.blank.data.bean.SystemImg;
import com.wisdudu.blank.data.repo.UserRepo;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.NextErrorSubscriber;
import com.wisdudu.blank.support.rxbus.RxBusFlag;
import com.wisdudu.blank.support.widget.loading.LoadingConstant;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by ypp on 2016/8/29.
 */
public class SelectSystemImgVM implements ViewModel {
    private SelectSystemImgFragment mFragment;

    private UserRepo userRepo;
    public final ObservableList<SystemImg> itemViewModel = new ObservableArrayList<>();
    public final ItemView itemView = ItemView.of(com.kelin.mvvmlight.BR.systemImg, R.layout.item_system_img_group);
    public final ReplyCommand onEmptyRetryCommand = new ReplyCommand(() -> {
        initData(true);
    });

    public final ReplyCommand onErrorRetryCommand = new ReplyCommand(() -> {
        initData(true);
    });

    public final ObservableField<Integer> pageStatus = new ObservableField<>();

    public SelectSystemImgVM(SelectSystemImgFragment selectSystemImgFragment) {
        userRepo = Injection.provideUserRepo();
        mFragment = selectSystemImgFragment;
        initData(false);
    }

    private void initData(boolean b) {
        userRepo.getSystemImg(b)
                .doOnSubscribe(() -> pageStatus.set(LoadingConstant.LOADING))
                .compose(mFragment.bindToLifecycle())
                .subscribe(new NextErrorSubscriber<List<SystemImg>>() {
                    @Override
                    public void onNext(List<SystemImg> systemImgs) {
                        if (systemImgs.size() == 0) {
                            pageStatus.set(LoadingConstant.EMPTY);
                        } else {
                            pageStatus.set(LoadingConstant.CONTENT);
                            for (SystemImg systemImg : systemImgs) {
                                List<PhotoEntity> photos = systemImg.photoEntities;
                                for (int j = 0; j < photos.size(); j++) {
                                    photos.get(j).index = j;
                                    photos.get(j).setOnItemClickListener(new PhotoEntity.onItemClickListener() {
                                        @Override
                                        public void onItemClick(PhotoEntity photoEntity) {
                                            Intent intent = new Intent(mFragment.mActivity, ImageGlanceActivity.class);
                                            ArrayList<String> pahts = new ArrayList<>();
                                            for (PhotoEntity photo : photos) {
                                                pahts.add(photo.getOriginalimg());
                                            }
                                            intent.putStringArrayListExtra(RxBusFlag.IMAGE_GLANCE_PATHS, pahts);
                                            intent.putExtra(RxBusFlag.CURRENT_PHOTO_INDEX, photoEntity.index);
                                            intent.putExtra(RxBusFlag.IS_SHOW_SAVE, true);
                                            mFragment.startActivityForResult(intent, SelectSystemImgFragment.REQUEST_SYSTEM_IMG);
                                        }
                                    });
                                }
                            }
                            itemViewModel.addAll(systemImgs);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        pageStatus.set(LoadingConstant.ERROR);
                    }
                });
    }
}
